<?php
/**
* Pager Helper
*/

class Zend_View_Helper_Paging
{
   /**
    * Current page
    * @var integer
    */
   protected $currentPage = 1;

   /**
    * Per page
    * How many items to display per pages.
    * @var integer
    */
   protected $perPage = 20;

   /**
    * Display links
    * How many page to display in the pagination links.
    * @var integer
    */
   protected $pageRange = 10;

   /**
    * Total number of items
    * @var integer
    */
   protected $total = 0;

   /**
    * Start page
    * @var integer
    */
   protected $startPage = 1;

   /**
    * End page
    * @var integer
    */
   protected $endPage = 1;

   public $option = array();
   /**
    * Pager format.
    */
   const FORMAT_FIRST              = '<a href="%s"><img src="[appurl]public/images/ico_next_n.gif" alt="Next"  title="Next"/></a>';   
    const FORMAT_PREVIOUS           = '<li><a href="%s" title="previous"><img src="[appurl]public/images/ico_arrow_prev.gif" alt="Previous" title="Previous"/></a></li>'; 
   const FORMAT_PREVIOUS_RANGE     = '<a href="%s" class="pager_previous_range">...</a>';
   const FORMAT_NAVIGATION_LINK    = '<li><a href="%s">%d</a></li>';
    const FORMAT_NAVIGATION_CURRENT = '<li>Page <strong>%d</strong> of <strong>%d</strong></li>'; 
   const FORMAT_NEXT_RANGE         = '<a href="%s" class="pager_next_range">...</a>';
    const FORMAT_NEXT               = '<li><a href="%s" title="next"><img src="[appurl]public/images/ico_arrow_next.gif" alt="next" title="next"/></a></li>'; 
   const FORMAT_LAST               = '<a href="%s"><img src="[appurl]public/images/ico_previous_n.gif" alt="Previous" title="Previous"/></a>';
   const FORMAT_NAVIGATION_LINK_LAST = '<li class="last"><a href="%s">%d</a></li>';
   /**
    * Base link formats
    * @var array
    */
   protected $baseFormats =
       array(
           'first'             => self::FORMAT_FIRST,
           'previous'          => self::FORMAT_PREVIOUS,
           'previousRange'     => self::FORMAT_PREVIOUS_RANGE,
           'navigationLink'    => self::FORMAT_NAVIGATION_LINK,
           'navigationCurrent' => self::FORMAT_NAVIGATION_CURRENT,
           'nextRange'         => self::FORMAT_NEXT_RANGE,
           'next'              => self::FORMAT_NEXT,
           'last'              => self::FORMAT_LAST
       );

   /**
    * Pager instance
    * @var object
    */
   static protected $paging;

   /**      
    * Pager
    *
    * @access public
    *
    * @param integer $total Total records
    * @param integer $perPage How many to display per page
    *
    * @return object pager
    */      
   public function reset(){
        self::$paging = null;
   } 
   
   public function paging($page = 1, $total = null, $perPage = 20, $option=array())
   {
       if(isset(self::$paging)) return self::$paging;
       
       if($page >= 0)
            $this->currentPage = $page;           
       else 
            $this->currentPage = 1;
        
       // Number of total data.
       $this->total = $total;
       
       // How many rows to display per page.
       $this->perPage = $perPage;

       // Number of total page
       $this->totalPage = ceil($this->total / $this->perPage);

       // If total page is smaller than called page.
       if ($this->totalPage < $this->currentPage)        
            $this->currentPage = $this->totalPage;
            
       // Page numnber of core pages.
       $corePage = ceil($this->currentPage / $this->pageRange);

       // Max page number.
       $this->endPage = ($this->pageRange * $corePage);

       // Start page number.
       $this->startPage = ($this->endPage - $this->pageRange) + 1;
       
       $this->option = $option;
          
       // Set to static instance
       self::$paging = $this;
        
       return $this;
   }  

   public function show_paging()
   {
       if( $this->totalPage > 1 ) 
        return 1;
       else 
        return 0;
   }
   /**
    * Display a simple pagination ex:[<< < ... 1 2 3 ... > >>]
    *
    * @param  array $formats An array of links to use sprintf()
    * @return string A link to the desired page page of the pagination
    */
   public function paginate(array $formats = array())
   {
       $formats = array_merge($this->baseFormats, $formats);
       
       $this->pager($this->total, $this->perPage);
       $pagination = array();
       $pagination[] = $this->pager()->first($formats['first']);
       $pagination[] = $this->pager()->previous($formats['previous']);
       $pagination[] = $this->pager()->previousRange($formats['previousRange']);
       $pagination[] = $this->pager()->navigation($formats['navigationLink'], $formats['navigationCurrent']);
       $pagination[] = $this->pager()->nextRange($formats['nextRange']);
       $pagination[] = $this->pager()->next($formats['next']);
       $pagination[] = $this->pager()->last($formats['last']);

       return join('', $pagination);
   }

   /**
    * Display link of the navigation links ex:[1 2 3]
    *
    * @param  string $linkFormat Format string to display page number links to use sprintf()
    * @param  string $currentFormat Format string to display current page to use sprintf()
    * @return string A link to the desired page page of the pagination
    */
    /*
   public function navigation($linkFormat = self::FORMAT_NAVIGATION_LINK, $currentFormat = self::FORMAT_NAVIGATION_CURRENT, $linkFormatLast = self::FORMAT_NAVIGATION_LINK_LAST)
   {
           
       $return = null;
       if($this->totalPage<=1)
        return null;
       // Display [1,2,3__]
       for ($p = $this->startPage; $p <= $this->endPage; $p++) {
           // Unlink if the page is selected
           if ($p <= $this->totalPage) {
               if ($this->currentPage != $p) {
                  
                   $url = $this->url(array('page' => $p));
                    if($p < $this->totalPage && $p < $this->endPage)
                        $return[] = sprintf($linkFormat, $url, $p);
                    else
                        $return[] = sprintf($linkFormatLast, $url, $p);
               } else {
                  
                   if($this->currentPage == $this->totalPage )
                      $return[] = sprintf($linkFormatLast,"", $p);
                   else
                        $return[] = sprintf($currentFormat, $p);
               }
           }
       }
       return join('', $return);
   }  */
   public function navigation($linkFormat = self::FORMAT_NAVIGATION_CURRENT)
   {   
        return sprintf($linkFormat, $this->currentPage, $this->totalPage);                  
   } 

   /**
    * Display link of the next range ex:[...]
    *
    * @param  string $format Format string of link to use sprintf()
    * @return string A link to the previous range of the pagination
    */
   public function previousRange($format = self::FORMAT_PREVIOUS_RANGE)
   {
       $appurl = Zend_Registry::get("appSetting")->myid->appurl;
       $format = str_replace("[appurl]",$appurl,$format)  ;
       $return = null;
       if ($this->pageRange < $this->currentPage) {
           $url = $this->url(array('page' => ($this->startPage - 1)));
           $return = sprintf($format, $url);
       }
       return $return;
   }

   /**
    * Display link of the next range ex:[...]
    *
    * @param  string $format Format string of link to use sprintf()
    * @return string A link to the next range of the pagination
    */
   public function nextRange($format = self::FORMAT_NEXT_RANGE)
   {
       $appurl = Zend_Registry::get("appSetting")->myid->appurl;
       $format = str_replace("[appurl]",$appurl,$format)  ;
       $return = null;
       if ($this->endPage < $this->totalPage) {
           $url = $this->url(array('page' => ($this->endPage + 1)));
           $return = sprintf($format, $url);
       }
       return $return;
   }

   /**
    * Display link of the previous page ex:[<<]      
    *      
    * @param  string $format Format string of link to use sprintf()      
    * @return string A link to the first page of the pagination      
    */     
   public function first($format = self::FORMAT_FIRST)     
   {
       $appurl = Zend_Registry::get("appSetting")->myid->appurl;
       $format = str_replace("[appurl]",$appurl,$format)  ;
       $return = null;
       if ($this->currentPage != 1) {
           $url = $this->url(array('page' => 1));
           $return = sprintf($format, $url);
       }
       return $return;
   }

   /**
    * Display link of the previous page ex:[<]      
    *      
    * @param  string $format Format string of link to use sprintf()      
    * @return string A link to the previous page of the pagination      
    */
   public function previous($page_name, $format = self::FORMAT_PREVIOUS)
   {
       $appurl = Zend_Registry::get("appSetting")->callahorn->appurl;
       $format = str_replace("[appurl]",$appurl,$format)  ;
            
       $return = null;       
       if ( $this->currentPage != 1 && $this->currentPage != 0 ) {
           $this->option[$page_name] = $this->currentPage - 1;
           $url = $this->url($this->option);           
           $return = sprintf($format, $url); 
       } else {
           $format = str_replace("%s","#",$format);
           $format = str_replace(".gif","_end.gif",$format);
           return $format;           
       }
       return $return;
   }     
   
   public function previous_ajax($javascript, $format = self::FORMAT_PREVIOUS)
   {
       $appurl = Zend_Registry::get("appSetting")->callahorn->appurl;
       $format = str_replace("[appurl]",$appurl,$format)  ;
            
       $return = null;       
       if ( $this->currentPage != 1 && $this->currentPage != 0 ) {
            $format = str_replace("%s",$javascript,$format); 
       } else {                                        
           $format = str_replace("%s","#",$format);      
           $format = str_replace(".gif","_end.gif",$format);           
       }
       return $format;
   }

   /**
    * Display link of the next page ex:[>]
    *
    * @param  string $format Format string of link to use sprintf()
    * @return string A link to the next page of the pagination
    */
   public function next($page_name, $format = self::FORMAT_NEXT)
   {
       $appurl = Zend_Registry::get("appSetting")->callahorn->appurl;
       $format = str_replace("[appurl]",$appurl,$format)  ;
       
       $return = null;
       if($this->currentPage < $this->totalPage) {
           $this->option[$page_name] = $this->currentPage + 1;
           $url = $this->url($this->option);
           $return = sprintf($format, $url);
       } else {
           $format = str_replace("%s","#",$format);
           $format = str_replace(".gif","_end.gif",$format);
           return $format;           
       }
       return $return;
   }

   public function next_ajax($javascript, $format = self::FORMAT_NEXT)
   {
       $appurl = Zend_Registry::get("appSetting")->callahorn->appurl;
       $format = str_replace("[appurl]",$appurl,$format)  ;
       
       $return = null;
       if($this->currentPage < $this->totalPage) {
           $format = str_replace("%s",$javascript,$format);             
       } else {
           $format = str_replace("%s","#",$format);
           $format = str_replace(".gif","_end.gif",$format);
           
       }
       return $format;           
   }
   /**
    * Display link of the last page ex:[>>]
    *
    * @param  string $format Format string of link to use sprintf()
    * @return string A link to the last page of the pagination
    */
   public function last($format = self::FORMAT_LAST)
   {       
       $return = null;
       if($this->currentPage != $this->totalPage) {
           $url = $this->url(array('page' => $this->totalPage));
           $return = sprintf($format, $url);
       }
       return $return;
   }


   /**
    * Generates an url given the name of a route.
    *
    * @access public
    *
    * @see    Zend_View_Helper::url()
    * @param  array $urlOptions Options passed to the assemble method of the Route object.
    * @param  mixed $name The name of a Route to use. If null it will use the current Route
    * @param  bool $reset Whether or not to reset the route defaults with those provided
    * @return string Url for the link href attribute.
    */
   public function url(array $urlOptions = array(), $name = null, $reset = false)
   {                
       $obj = new Zend_View_Helper_Url();                                                                                                                                      
       $url = $obj->url($urlOptions, $name, $reset); 
       return $url;                                      
   }

}